package com.web.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Model;
import org.apache.log4j.Logger;

import java.util.Date;
import java.util.List;

/**
 * Created by reeveslee on 16/7/3.
 */
public class CardLaunchRecord extends Model<CardLaunchRecord> {
    private static final Logger LOGGER = Logger.getLogger(CardLaunchRecord.class);
    public static CardLaunchRecord cardLaunchRecordDao = new CardLaunchRecord();

    public List<CardLaunchRecord> getUserLaunchCardRecords(long userId) {
        String sql = "select id,type,date from v1_grass_launchcard where userId=? order by date desc";
        return cardLaunchRecordDao.find(sql, userId);
    }

    /**
     * 添加打卡记录 - 公里单位
     */
    public long addLaunchRecordMile(int userId, JSONArray pic, double mile, String wayStr, Date time, String weather, String text) {
        CardLaunchRecord cardLaunchRecord = new CardLaunchRecord();
        cardLaunchRecord.set("userId", userId).set("photo", pic.toJSONString()).set("date", time).set("type", wayStr).set("value", mile).set("weather", weather).set("text", text).save();
        return cardLaunchRecord.get("id");
    }

    /**
     * 添加打卡记录 - 步数单位
     */
    public long addLaunchRecordStep(int userId, JSONArray pic, int step, String wayStr, Date time, String weather, String text) {
        CardLaunchRecord cardLaunchRecord = new CardLaunchRecord();
        cardLaunchRecord.set("userId", userId).set("photo", pic.toJSONString()).set("date", time).set("type", wayStr).set("value", step).set("weather", weather).set("text", text).save();
        return cardLaunchRecord.get("id");
    }

    /**
     * 添加打卡记录 - 时间单位
     */
    public long addLaunchRecordTime(int userId, JSONArray pic, double time, String wayStr, Date date, String weather, String text) {
        CardLaunchRecord cardLaunchRecord = new CardLaunchRecord();
        cardLaunchRecord.set("userId", userId).set("photo", pic.toJSONString()).set("date", date).set("type", wayStr).set("value", time).set("weather", weather).set("text", text).save();
        return cardLaunchRecord.get("id");
    }

    /**
     * 获取打卡上传图片数组
     */
    public JSONArray getLaunchImgs(int launchId) {
        String imgstr = cardLaunchRecordDao.findById(launchId).get("photo");
        return JSONArray.parseArray(imgstr);
    }
}
